<?php
/**
 * 后台菜单管理
 * @author hxtgirq hxtgirq710@qq.com
 */
class MenuAction extends BaseAction{

	/**
	 * 菜单列表
	 */
	function index(){
		$where = array();
		foreach($_GET as $key=>$val){
			$this->assign('get_'.$key,$val);
		}
		if(isset($_GET['word']) && !empty($_GET['word']))
			$where['name'] = array('like','%'.$_GET['word'].'%');
		$list = D("Menu")->where($where)->order()->select();
		$this->assign('list',$list);
		$this->display('index');
	}

	/**
	 * 添加/编辑
	 */
	function data(){
		$sign = D("Menu")->where("id=".(int)$_GET['id'])->find();
		$this->assign('sign',$sign);
		$this->display('data');
	}

	/**
	 * 添加菜单
	 */
	function add_data(){
 		if(!IS_POST) exit('非法提交');
 		$db = D("Menu");
 		$result = $db->create() ? 
 			( $db->add() !== false ? 
 				array('state'=>1,'actMsg'=>'菜单添加成功') : 
 				array('state'=>0,'actMsg'=>'菜单添加失败') 
 			) : 
 			array('state'=>0,'actMsg'=>'验证未通过,错误信息:'.$db->getError());
 		echo json_encode($result);
	}

	/**
	 * 修改菜单
	 */
	function save_data(){
		if(!IS_POST) exit('非法提交');
 		$db = D("Menu");
 		$result = $db->create() ? 
 			( $db->where('id='.$_POST['id'])->save() !== false ? 
 				array('state'=>1,'actMsg'=>'菜单修改成功') : 
 				array('state'=>0,'actMsg'=>'菜单修改失败') 
 			) : 
 			array('state'=>0,'actMsg'=>'验证未通过,错误信息:'.$db->getError());
 		echo json_encode($result);
	}

	/**
	 * 删除菜单
	 */
	function del_data(){
		$id = $_REQUEST['id'];
		$rst = D("Menu")->where("id in($id)")->delete();
		echo json_encode(array(
			'state'=>$rst ? 1 : 0,
			'actMsg'=>$rst ? '菜单删除成功' : '菜单删除失败',
		));
	}
}
?>